<template>
  <el-dialog
    :title="changeType == '0' ? '添加' : '修改'"
    :visible.sync="dialogVisible"
    width="40%"
    :close-on-click-modal="false"
    @closed="dialogClosed"
  >
    <el-form :model="form" label-width="120px">
      <el-form-item label="账号">
        <el-input v-model="form.zh" placeholder="输入账号"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.mm" placeholder="输入密码"></el-input>
      </el-form-item>
      <el-form-item label="昵称">
        <el-input v-model="form.nc" placeholder="昵称"></el-input>
      </el-form-item>
      <el-form-item label="登录权限">
        <el-input v-model="form.dl" placeholder="登录权限"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    changeType: {
      type: String,
      default: () => "0"
    }
  },
  data() {
    return {
      dialogVisible: false,
      hasSubmit: false,
      form: {
        zh: "",
        mm: "",
        dl: "",
        nc: "",
        cjr: this.$store.state.userCode,
        pop: "0"
      }
    };
  },
  methods: {
    dialogClosed() {
      this.$emit("closed", this.hasSubmit);
    },
    dialogInit() {
      this.dialogVisible = true;
    },
    submit() {
      this.form.pop = this.changeType;
      const formData = new FormData();
      for (let i in this.form) {
        formData.set(i, this.form[i]);
      }
      this.$http
        .post(this.$baseUrl.adminManage, formData)
        .then(res => {
          console.log(res);
          if (!(res.data instanceof Array)) {
            return this.$message.error("服务器内部异常");
          }
          if (res.data.length > 0) {
            this.dialogVisible = false;
            if (res.data[0].pop == "1") {
              return this.$message.error(res.data[0].log);
            }
            this.hasSubmit = true;
            return this.$message.success(res.data[0].log);
          } else {
            return this.$message.error("服务器内部异常");
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>

<style></style>
